package walkera.cn.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.wk.smarthome.R;

import java.util.List;

import walkera.cn.UI.MaterialButtonFuck;
import walkera.cn.bean.AppDown;
import walkera.cn.callback.AppTypeSelectCallBack;


public class AppTypesAdapter extends RecyclerView.Adapter<AppTypesAdapter.ChooseHolder> {

    private List<AppDown> book ;
    private AppTypeSelectCallBack selectCallBack ;
    private int selectIndex = 0 ;
    private Context context ;
    public AppTypesAdapter(Context context, List<AppDown> book  , AppTypeSelectCallBack selectCallBack) {
        this.book = book;
        this.context = context;
        this.selectCallBack = selectCallBack ;
        this.setHasStableIds(true); //

    }


    @Override
    public ChooseHolder onCreateViewHolder(@Nullable ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.item_txt_choose, parent, false);
        return new ChooseHolder(view);
    }

    @Override
    public void onBindViewHolder(@Nullable ChooseHolder  holder,final int position) {
        AppDown bean = book.get(position);
        holder.item_txt.setText( bean.getTypeName()  );

        if(selectIndex==position){
            holder.item_txt.setTextColor(context.getResources().getColor(R.color.colorPrimaryDark));
        }else {
            holder.item_txt.setTextColor(Color.WHITE);
        }

    }


    @Override
    public int getItemCount() {
        return book==null? 0: book.size();
    }




    @Override
    public long getItemId(int position) {
        return position;
    }


        public class ChooseHolder extends RecyclerView.ViewHolder {

        private MaterialButtonFuck item_txt;
         private ChooseHolder(View itemView){
            super(itemView);
            item_txt =  itemView.findViewById(R.id.item_txt);
            View.OnClickListener clickListener = new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    int mSelectedItem = getAdapterPosition();

                    if(selectIndex== mSelectedItem){
                        return;
                    }

                    selectCallBack.setSelectedCallBack(book.get(mSelectedItem)  ,mSelectedItem);
                    selectIndex = mSelectedItem ;
                    notifyDataSetChanged() ;
                }
            };
            item_txt.setOnClickListener(clickListener);


        }
    }


}
